/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufpe.cin.dmo4.middleware.message;

import br.ufpe.cin.dmo4.middleware.io.DeepCopy;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Danilo
 */
public class MarshallerImpl implements Marshaller {

    @Override
    public RequestMessage unmarshalRequest(byte[] messageArray) {

        try {
            return (RequestMessage) DeepCopy.deserializeObject(messageArray);
        } catch (IOException ex) {
            Logger.getLogger(MarshallerImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MarshallerImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    @Override
    public ResponseMessage unmarshalResponse(byte[] messageArray) {
        try {
            return (ResponseMessage) DeepCopy.deserializeObject(messageArray);
        } catch (IOException ex) {
            Logger.getLogger(MarshallerImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MarshallerImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    @Override
    public byte[] marshall(Message message) {
        try {
            return DeepCopy.serializeObject(message);
        } catch (IOException ex) {
            Logger.getLogger(MarshallerImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public static void main(String[] args) {
        List<Object> list = new ArrayList<>();
        list.add("Ronaldo");
        list.add(12);

        Message m = new RequestMessage(1, true, "000", "toString", list);

        Marshaller mar = new MarshallerImpl();

        byte[] b = mar.marshall(m);

        System.out.println(new String(b));

        RequestMessage m2 = mar.unmarshalRequest(b);

        System.out.println(m2.getBody().get(1));
    }

}
